package com.starfactory.customer.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.starfactory.customer.shared.dto.CustomUserBo;
import com.starfactory.customer.shared.dto.Result;
import com.starfactory.customer.table.customUser.CustomUser;
import com.starfactory.customer.table.customUser.CustomUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class AddCustomApi {
  @Autowired
  private CustomUserMapper customuserMapper;

  public Result<String> addCustomApiApiFunction(CustomUserBo customUserBo) {
    // 通过客户唯一id客户信息是否存在
    QueryWrapper<CustomUser> customuserQueryWrapper = new QueryWrapper<>();
    customuserQueryWrapper.eq("customer_id", customUserBo.getUserId());
    CustomUser existingCustomuser = customuserMapper.selectOne(customuserQueryWrapper);
    if (existingCustomuser != null) {
      return new Result<>(-1, "客户id已经存在", null);
    }
    // 将查询到客户信息实体CustomUserBo转换为CustomUser，使用set赋值，并写入数据库中
    CustomUser customuser = new CustomUser();
    customuser.setTenantId(customUserBo.getTenantId());
    customuser.setRegionId(customUserBo.getRegionId());
    customuser.setPhoneNumber(customUserBo.getPhoneNumber());
    customuser.setMaintainer(customUserBo.getMaintainer());
    customuser.setDeleteFlag(0); // 假设0表示未删除

    customuser.setCreateTime(LocalDateTime.now());
    customuser.setUpdateTime(LocalDateTime.now());
    customuserMapper.insert(customuser);
    return new Result<>(0, "客户信息添加成功", null);
  }

}